package cnsatmsmart.automation.mmi.controller.event;

import cnsatmsmart.automation.mmi.helper.utilities.GlobalEnvVars;
import com.bbn.openmap.event.*;
import com.bbn.openmap.*;
import com.bbn.openmap.proj.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.JComboBox;

/*
 *
 * @author SIK - PTIK, BPPT
 */

public class ZoomMouseMode extends CoordMouseMode {
    protected double squareWidth = 100;
    public final static transient String modeID = "Zoom".intern();
    protected MapBean theMap = null;
    private JComboBox cbZoom;

    public ZoomMouseMode(JComboBox cbzoom) {
        super(modeID, true);
        this.cbZoom = cbzoom;
    }

    /**
     * Process a mouseClicked event.
     * Erase all drawn lines and circles upon a double mouse click
     * @param e mouse event.
     */
    public void mouseClicked(MouseEvent e) {
        if(e.getSource() instanceof MapBean){
            final int currX = e.getX();
            final int currY = e.getY();

            theMap = (MapBean)e.getSource();
            if ((currX > squareWidth) && (currY >squareWidth) && (currX < theMap.getBounds().getWidth() - 100) && (currY < theMap.getBounds().getHeight() - 100))
            {
                if(e.getClickCount() == 2){
                    if(e.getButton() == 1){
                        int squareCenterX = currX;
                        int squareCenterY = currY;
                        int squareUpperLeftX = currX - ((int)squareWidth/2);
                        int squareUpperLeftY = currY - ((int)squareWidth/2);
                        double aspect = (double)theMap.getHeight()/(double)theMap.getWidth();
                        double squareWidth = this.squareWidth;
                        double squareHeight = this.squareWidth;
                        if(aspect > 1){
                            squareHeight *= aspect;
                            squareUpperLeftY = squareCenterY - (int)(squareHeight/2);
                        }else{
                            squareWidth /= aspect;
                            squareUpperLeftX = squareCenterX - (int)(squareWidth/2);
                        }

                        Projection proj = theMap.getProjection();
                        LatLonPoint upperLeft = proj.inverse(squareUpperLeftX, squareUpperLeftY);
                        LatLonPoint lowerRight = proj.inverse(squareUpperLeftX + (int)(squareWidth), squareUpperLeftY + (int)(squareHeight));
                        LatLonPoint center = proj.inverse(squareCenterX, squareCenterY);
                        double necessaryScale = proj.getScale(upperLeft, lowerRight, proj.forward(upperLeft), proj.forward(lowerRight));
                        final Projection newProj = ProjectionFactory.makeProjection(Mercator.class, (float)center.getLatitude(), (float)center.getLongitude(), (float)necessaryScale, theMap.getWidth(), theMap.getHeight());

                        Thread delayThread = new Thread(){
                            public void run()
                            {
                                theMap.setProjection(newProj);
                            }
                        };

                        BufferedImage bi = theMap.getGraphicsConfiguration().createCompatibleImage(theMap.getWidth(), theMap.getHeight(), Transparency.OPAQUE);
                        theMap.paintAll(bi.getGraphics());
                        BufferedImage bi2 = bi.getSubimage(squareUpperLeftX, squareUpperLeftY, (int)squareWidth, (int)squareHeight);
                        BufferedImage square = new BufferedImage((int)squareWidth, (int)squareHeight, BufferedImage.TYPE_INT_RGB);
                        square.getGraphics().drawImage(bi2, 0, 0, (int)squareWidth, (int)squareHeight, null);
                        square.getGraphics().setColor(new Color(0, 255, 0, 255));
                        square.getGraphics().drawRect(0, 0, square.getWidth()-1, square.getHeight()-1);
                        delayThread.start();
                        double iterations = 10;
                        double widthIncrease = theMap.getWidth() - squareWidth;
                        double heightIncrease = theMap.getHeight() - squareHeight;
                        double widthInc = widthIncrease/iterations;
                        double heightInc = heightIncrease/iterations;
                        double leftInc = squareUpperLeftX/iterations;
                        double upInc = squareUpperLeftY/iterations;

                        for(int i=0; i<iterations+1; i++){
                            theMap.getGraphics().drawImage(square, squareUpperLeftX - (int)(leftInc*i), squareUpperLeftY - (int)(upInc*i), (int)squareWidth+(int)(widthInc*i), (int)squareHeight+(int)(heightInc*i), null);
                            try{
                                Thread.sleep(50);
                            }catch(Exception ex){}
                        }
                        float temp = ((GlobalEnvVars.maxScale-(float)necessaryScale)/4975000*100);
                        if (temp >= 1f)
                            this.cbZoom.setSelectedItem((int)temp+"%");
                        else
                        {
                            String str = String.format("%.2f", temp);
                            this.cbZoom.setSelectedItem(str+"%");
                        }
                    }
                }
            }
        }
    }

}

